package com.geo.geoProject.client.tousLesProgrammes.carte2d;

import com.geo.geoProject.client.util.geoLoc.ClientLocalization;
import com.geo.geoProject.client.util.geoLoc.ProcessWithLocation;
import com.google.gwt.ajaxloader.client.AjaxLoader;
import com.google.gwt.maps.client.MapType;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.event.MapClickHandler;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Carte2dPanel extends VerticalPanel {
	
	public MapWidget map;

	
	public Carte2dPanel() {
		setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		setSize("100%", "100%");


		
		AjaxLoader.loadApi("maps", "2", new Runnable() {

			public void run() {

				final MapWidget mapw ;
				mapw = new MapWidget(LatLng.newInstance(46.7097, 2.4609), 5, null);
				map = mapw ;
				mapw.setUIToDefault();
				mapw.setSize("100%", "100%");
//				Canvas canvas = desktop.getCanvas() ; 
//
//				final VerticalPanel vp = new VerticalPanel() ;
//				vp.add(mapw) ;

				add(mapw) ;
//				window.getChildren()[0].setTop("20px") ;


//				window.setDragAppearance(DragAppearance.TARGET);  
//				window.setShowDragShadow(true);  


//				window.addResizedHandler(new ResizedHandler() {
//					public void onResized(ResizedEvent event) {
//						int w = Math.max(window.getViewportWidth() , 10) ;
//						int h = Math.max(window.getViewportHeight()-40, 10) ;
//						vp.setWidth (w+"px") ;
//						vp.setHeight(h+"px") ;
//						mapw.setWidth (w+"px") ;
//						mapw.setHeight(h+"px") ;
//						window.getChildren()[0].resizeTo(w,h);
//					}
//				}) ;

//				window.setVisible(false);
//				canvas.addChild(window) ;

//				RootPanel.get().add(canvas);

//				final int w = Math.max(window.getViewportWidth() , 10) ;
//				final int h = Math.max(window.getViewportHeight()-40, 10) ;
//				vp.setSize  (w+"px", h   +"px") ;
//				mapw.setSize(w+"px", h+"px") ;
//				window.getChildren()[0].resizeTo(w,h);

				ClientLocalization.getPreciseLocAsync(new ProcessWithLocation() {
					public void run(double lat, double lng) {
						mapw.setCenter(LatLng.newInstance(lat, lng),15,MapType.getHybridMap()) ;
					}
				}) ;
//				
//				
//				getPositionAsync(new RunWithPosition() {
//					public void run(double lat, double lng) {
//						mapw.setCenter(LatLng.newInstance(lat, lng)) ;
//					}
//				}) ;
				mapw.addMapClickHandler(new MapClickHandler() {
					public void onClick(MapClickEvent event) {
						Window.alert("<"+mapw.getCenter().getLatitude()+" : "+mapw.getCenter().getLongitude()+"> ("+mapw.getZoomLevel()+")") ;
					}
				}) ;
				mapw.setCenter(LatLng.newInstance(46.7097, 2.4609), 5) ;
			}
		}, null);
		
		setSize("200px", "100px") ;
		
	}

}
